import develiver.RestfulMessageDeliverer;
import org.eclipse.californium.core.CoapServer;
import org.eclipse.californium.core.network.CoapEndpoint;
import org.eclipse.californium.core.network.EndpointManager;
import org.eclipse.californium.core.network.config.NetworkConfig;
import org.eclipse.californium.core.server.resources.Resource;

import java.net.InetAddress;
import java.net.InetSocketAddress;

/**
 * describe:
 *
 * @author helloworldyu
 * @data 2017/12/5
 */
public class CustomCoapServer extends CoapServer {
	public CustomCoapServer( int port, RestfulMessageDeliverer deliverer, Resource... resources) {
		//======设置资源========
		add(resources);
		//======设置分发器======
		deliverer.setRoot(getRoot());
		setMessageDeliverer(deliverer);
		//======监听端口========
		addEndpoints(port);
	}


	/**
	 * Add individual endpoints listening on default CoAP port on all IPv4
	 * addresses of all network interfaces.
	 */
	private void addEndpoints( int port ) {
		NetworkConfig config = NetworkConfig.getStandard();
		for (InetAddress addr : EndpointManager.getEndpointManager().getNetworkInterfaces()) {
			InetSocketAddress bindToAddress = new InetSocketAddress(addr, port);
			addEndpoint(new CoapEndpoint(bindToAddress, config));
		}
	}
}

